#include <gtest/gtest.h>

#include <QUuid>

#include <Type/News.h>
#include <Store/NewsTable.h>

#include <PredefinedConstance/News.h>
#include "CRUDTest.h"
#include "Database.h"

using namespace std;

const QString testSuiteName = "Store";
namespace Store
{
	class News
	{
	public:
		News()
		{
			Database();
		}
	public:
		typedef TCServer::Type::News DataType;
		typedef TCServer::Store::NewsTable TableType;
	public:
		typedef QList<DataType> QueryResult;
		QueryResult query()
		{
			return TableType::queryAll(testVals.groupId);
		}

		typedef quint64 InsertResult;
		quint64 insert()
		{
			return TableType::insert(testVals.val2insert);
		}

		typedef void UpdateResult;
		void update()
		{
			return TableType::update(testVals.val2update);
		}

		typedef void RemoveResult;
		void remove()
		{
			return TableType::remove(testVals.val2insert.id);
		}

	public:
		bool checkInsert(const QueryResult &requeryResult)
		{
			const auto &s = requeryResult;
			auto it = find_if(s.begin(), s.end(), [this](const DataType &v)
			{
				return v.contentUrl == testVals.val2insert.contentUrl;
			});
			if (it == s.end())
				return false;
			testVals.val2update.id = it->id;
			testVals.val2insert.id = it->id;
			return true;
		}
		bool checkRemove(const QueryResult &requeryResult)
		{
			const auto &s = requeryResult;
			auto it = find_if(s.begin(), s.end(), [this](const DataType &v)
			{
				return v.id == testVals.val2insert.id;
			});
			return it == s.end();
		}
	private:
		PredefinedConstance::News testVals;
	};
}

INSTANTIATE_TYPED_TEST_CASE_P(Store_News, CRUDTest, Store::News);